<?php

namespace app\admin\controller;

use app\admin\model\Orders;
use app\admin\model\OtherIncome;
use app\admin\model\Sales;
use app\admin\model\SalesDetail;
use app\common\controller\AdminController;
use EasyAdmin\annotation\ControllerAnnotation;
use EasyAdmin\annotation\NodeAnotation;
use think\App;
use think\facade\Cache;

/**
 * @ControllerAnnotation(title="数据统计")
 */
class Statistics extends AdminController
{

    public function __construct(App $app)
    {
        parent::__construct($app);
    }

    /**
     * @NodeAnotation(title="列表")
     */
    public function index()
    {
        $data = [
            'all_total_amount'      =>  Orders::cache(300)->sum('total_amount'),
            'all_pay_amount'        =>  Orders::cache(300)->sum('pay_amount'),
            'all_last_amount'       =>  Orders::cache(300)->sum('total_amount') - Orders::cache(300)->sum('pay_amount'),
            'all_other_amount'      =>  OtherIncome::cache(300)->sum('amount'),
            'today_total_amount'    =>  Orders::cache(300)->whereDay('create_time')->sum('total_amount'),
            'today_pay_amount'      =>  Orders::cache(300)->whereDay('create_time')->sum('pay_amount'),
            'today_other_amount'    =>  OtherIncome::cache(300)->whereDay('create_time')->sum('amount'),
            'yesterday_total_amount'=>  Orders::cache(300)->whereDay('create_time', 'yesterday')->sum('total_amount'),
            'yesterday_pay_amount'  =>  Orders::cache(300)->whereDay('create_time', 'yesterday')->sum('pay_amount'),
            'yesterday_other_amount'=>  OtherIncome::cache(300)->whereDay('create_time', 'yesterday')->sum('amount'),
            'month_total_amount'    =>  Orders::cache(300)->whereMonth('create_time')->sum('total_amount'),
            'month_pay_amount'      =>  Orders::cache(300)->whereMonth('create_time')->sum('pay_amount'),
            'month_other_amount'    =>  OtherIncome::cache(300)->whereMonth('create_time')->sum('amount'),
            'all_client_num'        =>  Sales::cache(300)->distinct(true)->field('name')->count(),
            'month_client_num'      =>  Sales::cache(300)->whereMonth('create_time')->distinct(true)->field('name')->count(),
            'today_client_num'      =>  Sales::cache(300)->whereDay('create_time')->distinct(true)->field('name')->count(),
            'yesterday_client_num'  =>  Sales::cache(300)->whereDay('create_time', 'yesterday')->distinct(true)->field('name')->count(),
        ];
        $total = 29;
        $chart_data['name'] = ['成交金额'];
        for ($i = $total; $i >= 0; $i--){
            $time = strtotime('-'.$i.' days');
            $cache = $i ? 3600 : false;
            $chart_data['day'][] = date('m-d', $time);
            $chart_data['total_amount'][] = Orders::whereTime('create_time','<=', $time)->cache($cache)->sum('total_amount');
        }

        $sales['today'] = Sales::whereDay('create_time')->count();
        $sales['all'] = Sales::count();

        $today_num = 0;
        $res = Sales::whereTime('create_time','<', strtotime(date('Y-m-d'), time()))->select();
        foreach ($res as $k => $v){
            $today_num += SalesDetail::whereDay('create_time','today')->where('sales_id', $v['id'])->count();
        }
        $sales['success'] = $today_num ? round($today_num / count($res) * 100,2) : 0;

        $error_num = Sales::where('status',1)->count();
        $sales['error'] = round($error_num / $sales['all'] * 100,2);

        $this->assign([
            'data'              =>  $data,
            'chart_data'        =>  json_encode($chart_data),
            'sales'             =>  $sales,
            'today_ranking'     =>  Cache::get('admin_ranking_type_0') ?: ranking(0,10),
            'yesterday_ranking' =>  Cache::get('admin_ranking_type_1') ?: ranking(1,10),
            'month_ranking'     =>  Cache::get('admin_ranking_type_2') ?: ranking(2,10),
        ]);
        return $this->fetch();
    }

}